package com.ruoyi.project.module.domain.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.module.domain.domain.Domain;
import com.ruoyi.project.module.domain.service.IDomainService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 域名管理 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-09-22
 */
@Controller
@RequestMapping("/module/domain")
public class DomainController extends BaseController
{
    private String prefix = "module/domain";
	
	@Autowired
	private IDomainService domainService;
	
	@RequiresPermissions("module:domain:view")
	@GetMapping()
	public String domain()
	{
	    return prefix + "/domain";
	}
	
	/**
	 * 查询域名管理列表
	 */
	@RequiresPermissions("module:domain:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Domain domain)
	{
		startPage();
        List<Domain> list = domainService.selectDomainList(domain);
		return getDataTable(list);
	}
	
	/**
	 * 新增域名管理
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存域名管理
	 */
	@RequiresPermissions("module:domain:add")
	@Log(title = "域名管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Domain domain)
	{
		if(StringUtils.isBlank(domain.getDescription())
				||domain.getPort()==null
				||StringUtils.isBlank(domain.getNamed())
				){
			return error(1, "描述,域名,端口不允许为空");
		}
		String userName = ShiroUtils.getUser().getLoginName();
		domain.setCreateBy(userName);
		domain.setCreateTime(new Date());
		domain.setUpdateBy(userName);
		domain.setUpdateTime(new Date());
		return toAjax(domainService.insertDomain(domain));
	}

	/**
	 * 修改域名管理
	 */
	@GetMapping("/edit/{apiDomainId}")
	public String edit(@PathVariable("apiDomainId") Integer apiDomainId, ModelMap mmap)
	{
		Domain domain = domainService.selectDomainById(apiDomainId);
		mmap.put("domain", domain);

	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存域名管理
	 */
	@RequiresPermissions("module:domain:edit")
	@Log(title = "域名管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Domain domain)
	{

		String userName = ShiroUtils.getUser().getLoginName();
		domain.setCreateBy(null);
		domain.setCreateTime(null);
		domain.setUpdateBy(userName);
		domain.setUpdateTime(new Date());
		return toAjax(domainService.updateDomain(domain));
	}
	
	/**
	 * 删除域名管理
	 */
	@RequiresPermissions("module:domain:remove")
	@Log(title = "域名管理", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(domainService.deleteDomainByIds(ids));
	}


	/**
	 * 接口项目中选择域名
	 */
	@GetMapping("/lookupDomainList/{domainId}")
	public String lookupDomainList(@PathVariable("domainId") Integer domainId, ModelMap mmap)
	{
	    if(domainId!=null){
			mmap.put("domain", domainService.selectDomainById(domainId));
		}
		return prefix + "/tree";
	}

	/**
	 * 加载部门列表树
	 */
	@PostMapping("/lookupDomainListData")
	@ResponseBody
	public TableDataInfo lookupDomainListData()
	{
		startPage();
		List<Domain> list = domainService.lookupDomainList();
		return getDataTable(list);
	}


	/**
	 * 复制配置组
	 */
	@GetMapping("/copy/{domainId}")
	public String copy(@PathVariable("domainId") Long domainId, ModelMap mmap)
	{
		Domain group = domainService.selectDomainById(domainId.intValue());
		mmap.put("domain", group);
		return prefix + "/copy";
	}


	/**
	 * 复制保存配置组
	 */
	@RequiresPermissions("module:group:add")
	@Log(title = "配置组", businessType = BusinessType.INSERT)
	@PostMapping("/copy")
	@ResponseBody
	public AjaxResult copySave(Domain domain)
	{
		return addSave(domain);
	}
}
